﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.Threading;
using Sinacor.OMM.HealthMonitor.Collector;
using Sinacor.OMM.HealthMonitor.Collector.SharedHealthDataMemoryStorage.OrderExecutor;
using Sinacor.OMM.Util;

namespace Sinacor.OMM.OrderExecutor
{
    //NEGAUT380_Proc

    public partial class OrderExecutorService : ServiceBase
    {

        Thread thOrdExecutor = null;
        Thread thBusExecutor = null;
        Thread thMatExecutor = null;
        Thread thFinExecutor = null;
        Thread thStockExecutor = null;

        ProcessLogic.OrderExecutor _orderExecutor = null;
        ProcessLogic.BusinessExecutor _businessExecutor = null;
        ProcessLogic.MatchExecutor _matchExecutor = null;
        ProcessLogic.FinancialExecutor _financialExecutor = null;
        ProcessLogic.StockExecutor _stockExecutor = null;
        private HealthMonitorCollector _healthMonitor;
        private OrderExecutorShdms _orderExecutorStatus;
        /// <summary>
        /// Constructor
        /// </summary>
        public OrderExecutorService()
        {
            InitializeComponent();
            this.ServiceName = "BM&FBOVESPA Order Executor";
        }

        /// <summary>
        /// On Start
        /// </summary>
        /// <param name="args"></param>
        protected override void OnStart(string[] args)
        {
            _healthMonitor = HealthMonitorCollector.GetInstance();
            _healthMonitor.AddHealthRequestListener(Collect, Sinacor.OMM.HealthMonitor.Entity.HealthType.Executor);
            _orderExecutorStatus = OrderExecutorShdms.GetSharedStorage();

            thOrdExecutor = new Thread(new ThreadStart(RunOrder));
            thBusExecutor = new Thread(new ThreadStart(RunBusiness));
            thMatExecutor = new Thread(new ThreadStart(RunMatch));
            thFinExecutor = new Thread(new ThreadStart(RunFinancial));
            thStockExecutor = new Thread(new ThreadStart(RunStock));

            thOrdExecutor.Name = "Order Executor";
            thOrdExecutor.Start();

            thBusExecutor.Name = "Business Executor";
            thBusExecutor.Start();

            thMatExecutor.Name = "Match Executor";
            thMatExecutor.Start();

            thFinExecutor.Name = "Financial Executor";
            thFinExecutor.Start();

            thStockExecutor.Name = "Stock Executor";
            thStockExecutor.Start();
        }

        /// <summary>
        /// Run order executor
        /// </summary>
        private void RunOrder()
        {
            _orderExecutor = new Sinacor.OMM.OrderExecutor.ProcessLogic.OrderExecutor();
            _orderExecutor.Run();
        }

        /// <summary>
        /// Run business executor
        /// </summary>
        private void RunBusiness()
        {
            _businessExecutor = new Sinacor.OMM.OrderExecutor.ProcessLogic.BusinessExecutor();
            _businessExecutor.Run();
        }

        /// <summary>
        /// Run business executor
        /// </summary>
        private void RunMatch()
        {
            _matchExecutor = new Sinacor.OMM.OrderExecutor.ProcessLogic.MatchExecutor();
            _matchExecutor.Run();
        }

        /// <summary>
        /// Run financial executor
        /// </summary>
        private void RunFinancial()
        {
            _financialExecutor = new Sinacor.OMM.OrderExecutor.ProcessLogic.FinancialExecutor();
            _financialExecutor.Run();
        }

        /// <summary>
        /// Run stock executor
        /// </summary>
        private void RunStock()
        {
            _stockExecutor = new Sinacor.OMM.OrderExecutor.ProcessLogic.StockExecutor();
            _stockExecutor.Run();
        }

        /// <summary>
        /// On Stop
        /// </summary>
        protected override void OnStop()
        {
            //
            if (_orderExecutor != null)
                _orderExecutor.Stop();

            if (_businessExecutor != null)
                _businessExecutor.Stop();

            if (_matchExecutor != null)
                _matchExecutor.Stop();

            if (_financialExecutor != null)
                _financialExecutor.Stop();

            if (_stockExecutor != null)
                _stockExecutor.Stop();

            if (_healthMonitor != null)
                _healthMonitor.Dispose();

            if (_orderExecutorStatus != null)
                _orderExecutorStatus.Dispose();

            Tracer.Destroy();
        }

        private object Collect()
        {
            return _orderExecutorStatus;
        }

    }
}
